﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
using System.Text;
namespace Catering_Rules
{
    /// <summary>
    /// Dynamic
    /// </summary>
    public partial class Dynamic
    {
        private readonly Catering_Operation.Dynamic dal = new Catering_Operation.Dynamic();
        public Dynamic()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.Dynamic model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.Dynamic model, bool Isxiugai)
        {
            dal.Add(model, Isxiugai);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.Dynamic model)
        {
            return dal.Update(model);
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteListBywhere(string where)
        {
            return dal.DeleteListBywhere(where);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.Dynamic GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.Dynamic GetModelByCache(int id)
        {

            string CacheKey = "DynamicModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.Dynamic)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.Dynamic> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.Dynamic> DataTableToList(DataTable dt)
        {
            List<Catering_Model.Dynamic> modelList = new List<Catering_Model.Dynamic>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.Dynamic model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.Dynamic();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["GroupID"] != null && dt.Rows[n]["GroupID"].ToString() != "")
                    {
                        model.GroupID = int.Parse(dt.Rows[n]["GroupID"].ToString());
                    }
                    if (dt.Rows[n]["ClassID"] != null && dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["Date"] != null && dt.Rows[n]["Date"].ToString() != "")
                    {
                        model.Date = DateTime.Parse(dt.Rows[n]["Date"].ToString());
                    }
                    if (dt.Rows[n]["Number"] != null && dt.Rows[n]["Number"].ToString() != "")
                    {
                        model.Number = int.Parse(dt.Rows[n]["Number"].ToString());
                    }
                    if (dt.Rows[n]["Catering"] != null && dt.Rows[n]["Catering"].ToString() != "")
                    {
                        model.Catering = int.Parse(dt.Rows[n]["Catering"].ToString());
                    }
                    if (dt.Rows[n]["Charge"] != null && dt.Rows[n]["Charge"].ToString() != "")
                    {
                        model.Charge = int.Parse(dt.Rows[n]["Charge"].ToString());
                    }
                    if (dt.Rows[n]["HPersons"] != null && dt.Rows[n]["HPersons"].ToString() != "")
                    {
                        model.HPersons = int.Parse(dt.Rows[n]["HPersons"].ToString());
                    }
                    if (dt.Rows[n]["QC"] != null && dt.Rows[n]["QC"].ToString() != "")
                    {
                        model.QC = int.Parse(dt.Rows[n]["QC"].ToString());
                    }
                    if (dt.Rows[n]["persons"] != null && dt.Rows[n]["persons"].ToString() != "")
                    {
                        model.persons = int.Parse(dt.Rows[n]["persons"].ToString());
                    }
                    if (dt.Rows[n]["Eat"] != null && dt.Rows[n]["Eat"].ToString() != "")
                    {
                        model.Eat = int.Parse(dt.Rows[n]["Eat"].ToString());
                    }
                    if (dt.Rows[n]["EatTID"] != null && dt.Rows[n]["EatTID"].ToString() != "")
                    {
                        model.EatTID = int.Parse(dt.Rows[n]["EatTID"].ToString());
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["temp2"] != null && dt.Rows[n]["temp2"].ToString() != "")
                    {
                        model.temp2 = dt.Rows[n]["temp2"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        /// <summary>
        /// 全年各班组就餐人数
        /// </summary>
        /// <param name="year">时间</param>
        /// <param name="classId">班组id，可以传入null</param>
        /// <returns></returns>
        public DataTable GetEat_Of_YearByTime(string strarealist, string[] year, string groupid, string classId)
        {
            return dal.GetEat_Of_YearByTime(strarealist, year, groupid, classId);
        }

        public DataTable GetEatNumbersByYear(string strclasslist, string year, string classsId)
        {
            return dal.GetEatNumbersByYear(strclasslist, year, classsId);
        }

        public decimal GetEatNumberByMonth(string CLASSID, string YEAR, string MONTH)
        {
            StringBuilder where = new StringBuilder();
            if (!CLASSID.Equals("-10"))
            {
                where.Append(string.Format(" and ClassID = {0}", CLASSID));
            }
            where.Append(string.Format(" and yearofdate={0}  and monthofdate = {1}", YEAR, MONTH));
            return dal.GetEatNumberByMonth(where.ToString());
        }

        #endregion  Method

        /// <summary>
        /// 各班组每月的总人数
        /// </summary>
        /// <returns></returns>
        public DataTable DynamicListGroupByMonth(string oAreaId, string classId, DateTime time)
        {
            StringBuilder wheredy = new StringBuilder();
            if (!string.IsNullOrEmpty(oAreaId) && !oAreaId.Equals("-10"))
            {
                wheredy.Append(string.Format(" and GroupID = {0}", oAreaId));
            }
            if (!string.IsNullOrEmpty(classId) && !classId.Equals("-10"))
            {
                wheredy.Append(string.Format(" and ClassID = {0}", classId));
            }
            if (time != null)
            {
                wheredy.Append(string.Format(" and yearofdate = {0}", time.Year));
            }
            return dal.GetEatNumbersByYear(wheredy.ToString());
        }
        /// <summary>
        /// 各班组每年的总人数
        /// </summary>
        /// <returns></returns>
        public DataTable GetEatNumbersCount(string classId, DateTime time)
        {
            StringBuilder wheredy = new StringBuilder();
            if (!string.IsNullOrEmpty(classId) && !classId.Equals("-10"))
            {
                wheredy.Append(string.Format(" and ClassID = {0}", classId));
            }
            if (time != null)
            {
                wheredy.Append(string.Format(" and yearofdate = {0}", time.Year));
            }
            return dal.GetEatNumbersCount(wheredy.ToString());
        }

        public DataTable GetCateringAndEatNumber(string AREAID, int ClassID, DateTime DTIME1, DateTime DTIME2)
        {
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(AREAID) && !AREAID.Equals("-10"))
            {
                where.Append(string.Format(" AND GroupID IN ({0}) ", AREAID));
            }
            if (ClassID > 0)
            {
                where.Append(string.Format(" AND ClassID ={0} ", ClassID));
            }

            if (DTIME1 != null && DTIME2 != null)
            {
                where.Append(string.Format(" AND date>='{0}' AND  date<'{1}'", DTIME1.ToString("yyyy-MM-dd"), DTIME2.AddMonths(1).ToString("yyyy-MM-dd")));
            }
            return dal.GetCateringAndEatNumber(where.ToString());
        }

        /// <summary>
        /// 获取服务人数
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="QC"></param>
        /// <param name="Persons"></param>
        /// <param name="Catering"></param>
        public void GetEatNumberByClassIDAndMonth(int classid, int year, int month, out int QC, out int Persons, out int Catering)
        {
            QC = 0;
            Persons = 0;
            Catering = 0;
            if (classid <= 0) return;
            if (year <= 0) return;
            if (month <= 0) return;
            string where = string.Format(" AND ClassID={0} AND Year(Date)={1} AND Month(Date)={2} ", classid, year, month);
            DataTable dt = dal.GetEatNumberByClassIDAndMonth(where);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (!(dt.Rows[0]["QC"] is DBNull)) QC = Convert.ToInt32(dt.Rows[0]["QC"]);
                if (!(dt.Rows[0]["Persons"] is DBNull)) Persons = Convert.ToInt32(dt.Rows[0]["Persons"]);
                if (!(dt.Rows[0]["Catering"] is DBNull)) Catering = Convert.ToInt32(dt.Rows[0]["Catering"]);
            }
            dt.Dispose();
        }
    }
}

